#include "EntityManager.h"
#include "Entity.h"

#include <iostream>

using namespace Game;
using namespace Totem;

EntityManager::~EntityManager()
{
	for(unsigned int i = 0; i < entities.size(); i++)
		delete entities[i];
}

void EntityManager::update(const float &deltaTime)
{
	if(!pendingDelete.empty())
	{
		for(unsigned int i = 0; i < pendingDelete.size(); i++)
		{
			for(unsigned int j = 0; j < entities.size(); j++)
			{
				if(entities[j] == pendingDelete[i])
				{
					//This is a vector element removal trick that's O(1)
					entities[j] = entities.back();
					entities.pop_back();
					break;
				}
			}
			delete pendingDelete[i];
		}
		pendingDelete.clear();
	}

	for(unsigned int i = 0; i < entities.size(); i++)
		entities[i]->updateComponents(deltaTime);
}

Entity &EntityManager::create()
{
	Entity *entity = new Entity(factory);
	entities.push_back(entity);
		return *entity;
}

void EntityManager::push_back(Entity *entity)
{
	entities.push_back(entity);
}

void EntityManager::erase(Entity *entity, bool pending)
{
	if(pending)
		pendingDelete.push_back(entity);
	else
	{
		for(unsigned int i = 0; i < entities.size(); i++)
		{
			if(entities[i] == entity)
			{
				entities[i] = entities.back();
				entities.pop_back();
				return;
			}
		}
	}
}
